<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>冒泡排序</title>
</head>
<body>
    <script>
        function dbSort(array){
            var len = array.length;
            for(var i = 0; i < len - 1; i++){
                // 为什么 j < len -1 -i ,因为每次便利都会把最大的放到最后，所以下一次遍历的时候，就不需要再比较了
                for(var j = 0; j < len - 1 - i; j++){
                    if(array[j] > array[j+1]){
                        var temp = array[j+1];
                        array[j+1] = array[j];
                        array[j] = temp;
                    }
                }
            }
            return array;
        }
        
        var array = [3,4,5,2,1];
        console.log(dbSort(array));
    </script>
</body>
</html>